package com.orange.api;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import server.rest.ContentType;
import server.rest.HttpContext;
import server.rest.HttpMethod;
import server.rest.ResponseException;
import server.rest.Rest;
import com.orange.api.dto.FeatureDto;
import common.ParamHelper;
import db.Connector;
import db.Row;
import db.RowSet;

public class HelloWorld
{
    private static final String ALIVE_TEXT = "I am alive";

    @Rest(method = HttpMethod.GET, schema = "/healthcheck", internal = false, responseContentType = ContentType.JSON)
    public HealthCheckResponse doGetHealthCheck(final HttpContext context) throws Exception
    {
        HealthCheckResponse hello = new HealthCheckResponse();
        hello.setAlive(ALIVE_TEXT);
        context.setStatusCode(HttpServletResponse.SC_OK);
        return hello;
    }

    @Rest(method = HttpMethod.POST, schema = "/healthcheck", internal = false, requestContentType = ContentType.JSON, responseContentType = ContentType.JSON)
    public HealthCheckResponse doPostHealthCheck(final HttpContext context, User user)
    {
        HealthCheckResponse hello = new HealthCheckResponse();
        hello.setAlive(String.format("User : %s  nDate : %s   %s", user.getName(), user.date, ALIVE_TEXT));
        context.setStatusCode(HttpServletResponse.SC_OK);
        return hello;
    }

    @Rest(method = HttpMethod.GET, schema = "/feature", internal = false, responseContentType = ContentType.JSON)
    public List<FeatureDto> doGetFeature(final HttpContext context) throws Exception
    {
        String sql = "select `id`, `name`, `type`, `description`, `effective` from feature ;";
        Connector conn = ApiRegister.getIntance().getCore();
        RowSet rs = conn.execute(sql, sql);
        List<FeatureDto> fList = new ArrayList<FeatureDto>();
        for (Row row : rs.getRows())
        {
            FeatureDto fd =
                            new FeatureDto(row.getInteger("id"), row.getString("name"), row.getString("type"),
                                            row.getString("description"), row.getDate("effective"));
            fList.add(fd);
        }
        return fList;
    }

    @Rest(method = HttpMethod.POST, schema = "/feature", internal = false, requestContentType = ContentType.JSON, responseContentType = ContentType.JSON)
    public void doPostFeature(final HttpContext context, FeatureDto dto) throws Exception
    {
        String insertSql = "INSERT INTO feature(`id`, `name`, `type`, `description`, `effective`) value(?,?,?,?,?);";
        Connector conn = ApiRegister.getIntance().getCore();
        conn.executeUpdate(insertSql, insertSql,
            new Object[] { dto.getId(), dto.getName(), dto.getType(), dto.getDesc(), dto.getEffective() });
    }

    @Rest(method = HttpMethod.DELETE, schema = "/feature", internal = false, responseContentType = ContentType.JSON)
    public void doDeleteFeature(final HttpContext context) throws ResponseException
    {
        
        throw new ResponseException(HttpServletResponse.SC_NOT_FOUND, "");
    }
    
    static List<FeatureDto> LIST = new ArrayList<FeatureDto>();

    static
    {
        LIST.add(new FeatureDto(1, "张三", "123456", "我从东方走来，要去西天取经！", new Date()));
        LIST.add(new FeatureDto(2, "张四", "9521324", "白日依山尽，黄河不到头", new Date()));
        LIST.add(new FeatureDto(3, "王五", "3334544", "小宇宙爆发，天马流星拳。", new Date()));
    }

    
}

class User
{
    Date date = new Date();
    String name;

    public Date getDate()
    {
        return date;
    }

    public void setDate(Date date)
    {
        this.date = date;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }
}

class HealthCheckResponse
{
    String alive;

    public String getAlive()
    {
        return alive;
    }

    public void setAlive(String alive)
    {
        this.alive = alive;
    }

}
